package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.operate.trading.api.parameter.AppSaveOrderBean;
import com.ymatou.operate.trading.api.service.AppSaveOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.ProductCallService;
import com.ymttest.business.service.YmtCallService;
import com.ymttest.common.enums.AppTerminalSourceEnum;
import com.ymttest.common.enums.LogisticsTypeEnum;
import com.ymttest.common.enums.OrderSourceEnum;
import com.ymttest.common.enums.OrderTypeEnum;
import com.ymttest.common.enums.TerminalSourceEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * App保存订单 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AppSaveOrder {
    private static AppSaveOrderBean appsaveorderbean;
    private static AppSaveOrderCall appsaveordercall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("App保存闪购订单");
    }

    @Before
    public void caseUp() {
        appsaveorderbean = new AppSaveOrderBean();
        appsaveordercall = new AppSaveOrderCall();

    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(appsaveordercall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    private void verifyDb() {
        try {
            DefaultWapper database = new DefaultWapper();
            int orderId = appsaveordercall.getOrderId();
            Logger.comment("校验订单号:" + orderId);
            // 订单主表
            List<Map> ymtOrders = database.selectOrderByOderId(orderId);
            // 订单商品明细
            List<Map> ymtOrdersInfoList = database.selectOrderInfoByOderId(orderId);
            // 订单扩展表
            List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);
            // 活动记录表
            List<Map> ymtOrderInfoExt = database.selectOrderInfoExtByOderId(orderId);
            // ymtOrderState 表
            List<Map> ymtOrderState = database.selectOrderStateByOderId(orderId);
            // ymt_tradinginfo 表
            int tradingId = (Integer) ymtOrders.get(0).get("iMainOrderId");
            // ymt_tradinginfo 表
            List<Map> ymtTradinginfo = database.selectTradinginfoByTradingId(tradingId);

            int productNum = 0;
            // 单个订单定金价格
            BigDecimal fOriginalPrice = new BigDecimal(0);
            // 单个订单商品价格
            BigDecimal fProductPrice = new BigDecimal(0);
            // 当前订单运费
            BigDecimal freight = new BigDecimal(0).setScale(2);
            // 当前订单优惠券code
            String couponCode = appsaveorderbean.getCouponCode();
            Logger.verifyEquals(couponCode, (String) ymtOrders
                    .get(0).get("sCouponCode"), "验证sCouponCode");
            // 当前订单优惠券类型
            Logger.verifyEquals(appsaveorderbean.getCouponUseType(),
                    (Integer) ymtOrders
                            .get(0).get("iCouponType"), "验证iCouponType");
            // 当前订单优惠券渠道
            Integer couponChannel =
                    appsaveorderbean.getCouponChannel();
            // 当前订单优惠券金额
            BigDecimal couponValue = new BigDecimal(0).setScale(2);
            if (appsaveorderbean.getCouponValue() != null) {
                couponValue = appsaveorderbean.getCouponValue()
                        .setScale(2);
                Logger.verifyEquals(
                        couponValue,
                        (BigDecimal) ymtOrders.get(0).get("CouponValue"), "验证CouponValue");
            }
            int rowId = 0;
            // 订单总成交价
            BigDecimal orderPrice = (BigDecimal) ymtOrders.get(0).get("fTotalPrice");
            // 订单总运费
            BigDecimal orderFreight = freight;
            // 订单总优惠券金额
            BigDecimal orderCouponAmount = couponValue;
            // 运费分摊比率= 订单总运费/订单总成交价
            BigDecimal freightRate = orderFreight.divide(orderPrice, 5, BigDecimal.ROUND_HALF_DOWN);
            // 优惠券分摊比率= 订单优惠券金额/订单总成交价
            BigDecimal couponRate = orderCouponAmount.divide(orderPrice, 5, BigDecimal.ROUND_HALF_DOWN);
            // 商品分摊的运费
            BigDecimal productFreight = new BigDecimal(0).setScale(2);
            // 商品分摊的优惠券金额
            BigDecimal productCouponAmount = new BigDecimal(0);

            Map ymtOrdersInfo = ymtOrdersInfoList.get(0);
            // 当前订单里商品的数量
            productNum = appsaveorderbean.getProductNum();

            Logger.comment("验证ymt_ordersInfo表");
            Logger.verifyEquals(1, ymtOrdersInfoList.size(), "验证ymt_ordersInfo表记录数");
            Logger.verifyEquals(
                    appsaveorderbean.getCatalogId(), (String) ymtOrdersInfo.get("sCatalogId"), "验证sCatalogId");
            Logger.verifyEquals(
                    appsaveorderbean.getProductPicUrl(), (String) ymtOrdersInfo.get("sPictureUrl"),
                    "验证sPictureUrl");
            Logger.verifyEquals(
                    appsaveorderbean.getEarnest().setScale(2),
                    (BigDecimal) ymtOrdersInfo.get("fOriginalPrice"), "验证fOriginalPrice");
            // 计算当前订单下所有商品定金 价格乘以数量
            fOriginalPrice = appsaveorderbean.getEarnest()
                    .multiply(new BigDecimal(productNum)).add(fOriginalPrice);
            Logger.verifyEquals(
                    appsaveorderbean.getProductNum()
                    , (Integer) ymtOrdersInfo.get("iAmount"), "验证iAmount");
            Logger.verifyEquals(
                    appsaveorderbean.getProductPrice()
                            .setScale(2),
                    (BigDecimal) ymtOrdersInfo.get("fProductPrice"), "验证fProductPrice");
            // 价格乘以数量
            fProductPrice = appsaveorderbean.getProductPrice().multiply(new BigDecimal(productNum)).add(fProductPrice);

            Logger.comment("验证Ymt_OrderInfo表计算优惠券,运费分摊");

            rowId++;
            BigDecimal productPrice =
                    appsaveorderbean
                            .getProductPrice();
            int proNum = appsaveorderbean
                    .getProductNum();

            Logger.debug("totalProPrice:" + orderPrice);
            Logger.debug("freight:" + orderFreight);
            if (rowId == ymtOrdersInfoList.size()) {
                productFreight = orderFreight;
                productCouponAmount = orderCouponAmount;
            }
            else {
                // 四舍五入，保留2位小数
                productFreight =
                        freightRate.multiply(productPrice).multiply(new BigDecimal(proNum))
                                .divide(new BigDecimal(1), 2, BigDecimal.ROUND_HALF_UP);
                orderFreight = orderFreight.subtract(productFreight);
                // 四舍五入，保留2位小数
                productCouponAmount =
                        couponRate.multiply(productPrice).multiply(new BigDecimal(proNum))
                                .divide(new BigDecimal(1), 2, BigDecimal.ROUND_HALF_UP);;
                orderCouponAmount = orderCouponAmount.subtract(productCouponAmount);
            }
            Logger.comment("计算优惠券,运费分摊");
            // 平台优惠券
            if (couponChannel == 1) {
                Logger.verifyEquals(
                        productCouponAmount,
                        (BigDecimal) ymtOrdersInfo.get("fYmtCouponAmount"),
                        "验证fYmtCouponAmount");
                Logger.verifyEquals(
                        new BigDecimal(0).setScale(2),
                        (BigDecimal) ymtOrdersInfo.get("fSellerCouponAmount"),
                        "验证fSellerCouponAmount");
            }
            // 商家优惠券
            if (couponChannel == 2) {
                Logger.verifyEquals(
                        productCouponAmount,
                        (BigDecimal) ymtOrdersInfo.get("fSellerCouponAmount"),
                        "验证fSellerCouponAmount");
                Logger.verifyEquals(
                        new BigDecimal(0).setScale(2),
                        (BigDecimal) ymtOrdersInfo.get("fYmtCouponAmount"),
                        "验证fYmtCouponAmount");
            }
            Logger.verifyEquals(
                    productFreight,
                    (BigDecimal) ymtOrdersInfo.get("fFreight"),
                    "验证fFreight");
            Logger.comment("验证ymt_orders表");
            Logger.verifyEquals(1, ymtOrders.size(), "验证ymt_orders表记录数");
            Logger.verifyEquals(appsaveorderbean.getBuyerId(), (Integer) ymtOrders.get(0).get("iUserId"),
                    "验证iUserId");
            // fOrderPrice=ymt_ordersInfo.fOriginalPrice*iAmount
            Logger.verifyEquals(fOriginalPrice.setScale(2), (BigDecimal) ymtOrders.get(0)
                    .get("fOrderPrice"), "验证fOrderPrice");
            Logger.verifyEquals(
                    freight,
                    (BigDecimal) ymtOrders.get(0).get("fFreight"), "验证fFreight");
            Logger.verifyEquals(1, (Integer) ymtOrders.get(0).get("iTradingStatus"), "验证iTradingStatus");
            Logger.verifyEquals(0, (Integer) ymtOrders.get(0).get("iUnfreezeStatus"), "验证iUnfreezeStatus");

            // fTotalPrice=ymt_ordersInfo.fProductPrice*iAmount
            Logger.verifyEquals(fProductPrice.setScale(2), (BigDecimal) ymtOrders.get(0)
                    .get("fTotalPrice"), "验证fTotalPrice");
            // 订单总价格 fPayableAmount=fTotalPrice+fFreight-CouponValue
            Logger.verifyEquals(fProductPrice.add(freight).subtract(couponValue), (BigDecimal) ymtOrders.get(0)
                    .get("fPayableAmount"), "验证fPayableAmount");
            // 平台优惠券
            if (couponChannel == 1) {
                Logger.verifyEquals(null, ymtOrders
                        .get(0)
                        .get("sSellerCouponCode"), "验证sSellerCouponCode");
                Logger.verifyEquals(new BigDecimal(0).setScale(2), ymtOrders
                        .get(0)
                        .get("fSellerCouponAmount"), "验证fSellerCouponAmount");
                Logger.verifyEquals(couponCode, (String) ymtOrders
                        .get(0)
                        .get("sYmtCouponCode"), "验证sYmtCouponCode");
                Logger.verifyEquals(couponValue, (BigDecimal) ymtOrders
                        .get(0)
                        .get("fYmtCouponAmount"), "验证fYmtCouponAmount");
            }
            // 商家优惠券
            if (couponChannel == 2) {
                Logger.verifyEquals(couponCode, (String) ymtOrders
                        .get(0)
                        .get("sSellerCouponCode"), "验证sSellerCouponCode");
                Logger.verifyEquals(couponValue, (BigDecimal) ymtOrders
                        .get(0)
                        .get("fSellerCouponAmount"), "验证fSellerCouponAmount");
                Logger.verifyEquals(null, (String) ymtOrders
                        .get(0)
                        .get("sYmtCouponCode"), "验证sYmtCouponCode");
                Logger.verifyEquals(new BigDecimal(0).setScale(2), (BigDecimal) ymtOrders
                        .get(0)
                        .get("fYmtCouponAmount"), "验证fYmtCouponAmount");
            }
            Logger.verifyEquals(tradingId, (Integer) ymtOrders
                    .get(0)
                    .get("iMainOrderId"), "验证iMainOrderId");
            Logger.verifyEquals(BigDecimal.ZERO.divide(BigDecimal.ONE, 2, 4), (BigDecimal) ymtOrders
                    .get(0)
                    .get("fSellerPromotionAmount"), "验证fSellerPromotionAmount");
            Logger.verifyEquals(
                    couponValue,
                    (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfCoupon"), "验证fPaidAmountOfCoupon");

            couponChannel =
                    appsaveorderbean.getCouponChannel();
            // 平台优惠券
            if (couponChannel == 1) {
                Logger.verifyEquals(
                        couponValue,
                        (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfYmtCoupon"),
                        "验证fPaidAmountOfYmtCoupon");
                Logger.verifyEquals(
                        new BigDecimal(0).setScale(2),
                        (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfSellerCoupon"),
                        "验证fPaidAmountOfSellerCoupon");
            }
            // 商家优惠券
            if (couponChannel == 2) {
                Logger.verifyEquals(
                        couponValue,
                        (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfSellerCoupon"),
                        "验证fPaidAmountOfSellerCoupon");
                Logger.verifyEquals(
                        new BigDecimal(0).setScale(2),
                        (BigDecimal) ymtOrderState.get(0).get("fPaidAmountOfYmtCoupon"),
                        "验证fPaidAmountOfYmtCoupon");
            }

            Logger.comment("验证Ymt_Tradinginfo表");
            Logger.verifyEquals(
                    tradingId,
                    (Integer) ymtTradinginfo.get(0).get("iTradingId"), "验证iTradingId");
            Logger.verifyEquals(
                    appsaveorderbean.getBuyerId(),
                    (Integer) ymtTradinginfo.get(0).get("iUserId"), "验证iUserId");
            Logger.verifyEquals(
                    1,
                    (Integer) ymtTradinginfo.get(0).get("iTradingStatus"), "验证iTradingStatus");
            // 交易应付金额=总订金+总运费-总优惠券金额
            BigDecimal payAmount = fOriginalPrice.add(freight).subtract(couponValue);
            Logger.verifyEquals(payAmount, (BigDecimal) ymtTradinginfo.get(0).get("fAmount"),
                    "验证fAmount");
            Logger.verifyEquals(payAmount, (BigDecimal) ymtTradinginfo.get(0).get("fPayableAmount"),
                    "验证fPayableAmount");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_AppSaveOrder_001() {
        Logger.start(true, "App保存闪购订单成功");
        try {
            /*** 读取测试数据 ***/
            Logger.comment("前置数据准备");
            int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            String BuyUserName = EnvSetup.getData("BuyUserName");
            String BuyUserPassWord = EnvSetup.getData("BuyUserPassword");
            // 买手信息
            int SellerId = Integer.valueOf(EnvSetup.getData("BuyerId"));
            String SellerName = EnvSetup.getData("BuyerName");
            // 买手登录
            Map m = AuthCallService.authLogin(SellerName, "123456");
            String sellerAccessToken = (String) m.get("accessToken");
            // 买家登录
            Map m2 = AuthCallService.authLogin(BuyUserName, "123456");
            String buyerAccessToken = (String) m.get("accessToken");

            YmtCallService ymt = new YmtCallService();
            /** 添加收货地址 ***/
            int AddressId;
            List AddressList = ymt.getAddressId(buyerAccessToken, UserId);
            if (AddressList.size() > 0) {
                AddressId = (Integer) AddressList.get(0);
            }
            else {
                ymt.addAddress(buyerAccessToken, UserId);
                AddressId = (Integer) ymt.getAddressId(buyerAccessToken, UserId).get(0);
            }
            Map map2 = AuthCallService.authLogin(BuyUserName, BuyUserPassWord);
            String AccessToken = (String) map2.get("AccessToken");
            // 创建产品
            /*** 创建直播商品 ***/
            Map productinfomap = ProductCallService.createC2CProduct(sellerAccessToken, 1);
            Logger.comment("前置数据准备结束");
            /*** 获取产品信息 ***/
            /*
             * String productId = (String) (productinfomap.get("productId")); String catalogId =
             * (String) productinfomap.get("catalogId"); int activityId = (Integer)
             * productinfomap.get("activityId"); String sPicUrl =
             * String.valueOf(productinfomap.get("pic")); String ProductName =
             * String.valueOf(productinfomap.get("productDesc")); BigDecimal fQuotePrice =
             * (BigDecimal) productinfomap.get("price");
             */


            /*** 获取产品信息 ***/
            String productId = TestDataManager.getRandomUUID().toString();
            String catalogId = TestDataManager.getRandomUUID().toString();
            int activityId = 123456789;
            String sPicUrl = "http://p243.img.ymatou.com:10243/upload/productdes/3afa103d4ec4466f824b9f0458d6f4e2.jpg";
            String ProductName = "接口自动化测试Test";
            BigDecimal fQuotePrice = new BigDecimal(160);
            appsaveorderbean.setActivityId(activityId);
            // 模板ID
            int ActivityTemplateId = 0;
            appsaveorderbean.setActivityTemplateId(ActivityTemplateId);
            appsaveorderbean.setAddressId(AddressId);
            // 应用终端来源
            appsaveorderbean.setAppTerminalSource(AppTerminalSourceEnum.PC);
            // 自动取消时间
            BigDecimal time = new BigDecimal("100");
            appsaveorderbean.setAutoCancelOrderMinutes(time);
            // 买家Id
            appsaveorderbean.setBuyerId(UserId);
            // 商品规格Id
            appsaveorderbean.setCatalogId(catalogId);
            // 佣金比率
            BigDecimal Rate = new BigDecimal("0.00");
            appsaveorderbean.setCommissionRate(Rate);
            // 是否使用优惠券
            appsaveorderbean.setCouponAvail4OrderDeduct(false);
            // 优惠券code
            appsaveorderbean.setCouponCode(null);
            // 商品定金价格
            appsaveorderbean.setEarnest(new BigDecimal(100));
            // 是否免佣金
            appsaveorderbean.setFeeFree(true);
            // 是否使用红包
            appsaveorderbean.setGiftAvail4OrderDeduct(false);
            // 是否返红包
            appsaveorderbean.setGiftAvail4Reward(false);
            // 订单留言
            appsaveorderbean.setLeaveWord("自动化测试订单");
            // 物流类型
            appsaveorderbean.setLogisticsType(LogisticsTypeEnum.XloboBondedDelivery);
            // 是否为V账户
            appsaveorderbean.setOnly4Vip(false);
            // 订单来源
            appsaveorderbean.setOrderSource(OrderSourceEnum.PC);
            // 订单类型
            appsaveorderbean.setOrderType(OrderTypeEnum.Shangou);
            // 商品规格
            String ProductCatalog = "户外装备";
            appsaveorderbean.setProductCatalog(ProductCatalog);
            // 商品Id
            appsaveorderbean.setProductId(productId);
            // 商品名称
            appsaveorderbean.setProductName(ProductName);
            // 购买的商品数
            int ProductNum = 1;
            appsaveorderbean.setProductNum(ProductNum);
            // 商品的图片地址
            appsaveorderbean.setProductPicUrl(sPicUrl);
            // 订单商品价格
            appsaveorderbean.setProductPrice(fQuotePrice);
            // 促销金额
            BigDecimal Promotion = new BigDecimal("0");
            appsaveorderbean.setPromotion(Promotion);
            // 促销类型
            int PromotionType = 0;
            appsaveorderbean.setPromotionType(PromotionType);
            // 卖家用户Id
            appsaveorderbean.setSellerId(SellerId);
            // 订单来源IP
            String ip = "192.168.0.1";
            appsaveorderbean.setSourceIP(ip);
            // 终端来源
            appsaveorderbean.setTerminalSource(TerminalSourceEnum.IOS);
            // 使用红包的数量
            int UseGiftAmount = 0;
            appsaveorderbean.setUseGiftAmount(UseGiftAmount);
            // 发送请求
            appsaveordercall.setData(appsaveorderbean);
            appsaveordercall.callService();
            Logger.verifyEquals(ECode.SUCCESS, appsaveordercall.getCode(), "验证Code");
            Logger.verifyNotNull(appsaveordercall.getOrderId(), "验证订单号不为空");
            // 数据库验证
            DefaultWapper database = new DefaultWapper();
            Logger.verifyEquals(1, database.selectOrderByOderId(appsaveordercall.getOrderId()).size(), "验证ymt_orders表");
            Logger.verifyEquals(1, database.selectOrderExtByOderId(appsaveordercall.getOrderId()).size(),
                    "验证ymt_ordersExt表");
            Logger.verifyEquals(1, database.selectOrderStateByOderId(appsaveordercall.getOrderId()).size(),
                    "验证ymt_ordersState表");
            Logger.verifyEquals(1, database.selectOrderInfoByOderId(appsaveordercall.getOrderId()).size(),
                    "验证ymt_ordersInfo表");
            Logger.verifyEquals(1, database.selectOrderInfoExtByOderId(appsaveordercall.getOrderId()).size(),
                    "验证ymt_ordersInfoExt表");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_AppSaveOrder_002() {
        Logger.start(true, "收货地址不存在,保存订单失败");
        try {
            /*** 读取测试数据 ***/
            Logger.comment("前置数据准备");
            int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            String BuyUserName = EnvSetup.getData("BuyUserName");
            String BuyUserPassWord = EnvSetup.getData("BuyUserPassword");
            // 买手信息
            int SellerId = Integer.valueOf(EnvSetup.getData("BuyerId"));
            String SellerName = EnvSetup.getData("BuyerName");
            // 买手登录
            Map m = AuthCallService.authLogin(SellerName, "123456");
            String sellerAccessToken = (String) m.get("accessToken");
            // 买家登录
            Map m2 = AuthCallService.authLogin(BuyUserName, "123456");
            String buyerAccessToken = (String) m.get("accessToken");

            YmtCallService ymt = new YmtCallService();
            /** 添加收货地址 ***/
            int AddressId = -1;
            Map map2 = AuthCallService.authLogin(BuyUserName, BuyUserPassWord);
            String AccessToken = (String) map2.get("AccessToken");
            // 创建产品
            /*** 创建直播商品 ***/
            Map productinfomap = ProductCallService.createC2CProduct(sellerAccessToken, 1);
            Logger.comment("前置数据准备结束");
            /*** 获取产品信息 ***/
            /*
             * String productId = (String) (productinfomap.get("productId")); String catalogId =
             * (String) productinfomap.get("catalogId"); int activityId = (Integer)
             * productinfomap.get("activityId"); String sPicUrl =
             * String.valueOf(productinfomap.get("pic")); String ProductName =
             * String.valueOf(productinfomap.get("productDesc")); BigDecimal fQuotePrice =
             * (BigDecimal) productinfomap.get("price");
             */

            /*** 获取产品信息 ***/
            String productId = TestDataManager.getRandomUUID().toString();
            String catalogId = TestDataManager.getRandomUUID().toString();
            int activityId = 123456789;
            String sPicUrl = "http://p243.img.ymatou.com:10243/upload/productdes/3afa103d4ec4466f824b9f0458d6f4e2.jpg";
            String ProductName = "接口自动化测试Test";
            BigDecimal fQuotePrice = new BigDecimal(160);
            appsaveorderbean.setActivityId(activityId);
            // 模板ID
            int ActivityTemplateId = 0;
            appsaveorderbean.setActivityTemplateId(ActivityTemplateId);
            appsaveorderbean.setAddressId(AddressId);
            // 应用终端来源
            appsaveorderbean.setAppTerminalSource(AppTerminalSourceEnum.PC);
            // 自动取消时间
            BigDecimal time = new BigDecimal("100");
            appsaveorderbean.setAutoCancelOrderMinutes(time);
            // 买家Id
            appsaveorderbean.setBuyerId(UserId);
            // 商品规格Id
            appsaveorderbean.setCatalogId(catalogId);
            // 佣金比率
            BigDecimal Rate = new BigDecimal("0.00");
            appsaveorderbean.setCommissionRate(Rate);
            // 是否使用优惠券
            appsaveorderbean.setCouponAvail4OrderDeduct(false);
            // 优惠券code
            appsaveorderbean.setCouponCode(null);
            // 商品定金价格
            appsaveorderbean.setEarnest(new BigDecimal(100));
            // 是否免佣金
            appsaveorderbean.setFeeFree(true);
            // 是否使用红包
            appsaveorderbean.setGiftAvail4OrderDeduct(false);
            // 是否返红包
            appsaveorderbean.setGiftAvail4Reward(false);
            // 订单留言
            appsaveorderbean.setLeaveWord("自动化测试订单");
            // 物流类型
            appsaveorderbean.setLogisticsType(LogisticsTypeEnum.XloboBondedDelivery);
            // 是否为V账户
            appsaveorderbean.setOnly4Vip(false);
            // 订单来源
            appsaveorderbean.setOrderSource(OrderSourceEnum.PC);
            // 订单类型
            appsaveorderbean.setOrderType(OrderTypeEnum.Shangou);
            // 商品规格
            String ProductCatalog = "户外装备";
            appsaveorderbean.setProductCatalog(ProductCatalog);
            // 商品Id
            appsaveorderbean.setProductId(productId);
            // 商品名称
            appsaveorderbean.setProductName(ProductName);
            // 购买的商品数
            int ProductNum = 1;
            appsaveorderbean.setProductNum(ProductNum);
            // 商品的图片地址
            appsaveorderbean.setProductPicUrl(sPicUrl);
            // 订单商品价格
            appsaveorderbean.setProductPrice(fQuotePrice);
            // 促销金额
            BigDecimal Promotion = new BigDecimal("0");
            appsaveorderbean.setPromotion(Promotion);
            // 促销类型
            int PromotionType = 0;
            appsaveorderbean.setPromotionType(PromotionType);
            // 卖家用户Id
            appsaveorderbean.setSellerId(SellerId);
            // 订单来源IP
            String ip = "192.168.0.1";
            appsaveorderbean.setSourceIP(ip);
            // 终端来源
            appsaveorderbean.setTerminalSource(TerminalSourceEnum.IOS);
            // 使用红包的数量
            int UseGiftAmount = 0;
            appsaveorderbean.setUseGiftAmount(UseGiftAmount);
            // 发送请求
            appsaveordercall.setData(appsaveorderbean);
            appsaveordercall.callService();
            Logger.verifyEquals(ECode.FAILED201, appsaveordercall.getCode(), "验证Code");
            Logger.verifyEquals(ECode.FAILED201_MSG_32, appsaveordercall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_AppSaveOrder_003() {
        Logger.start(true, "App保存闪购订单,使用商家优惠券");
        try {
            /*** 读取测试数据 ***/
            Logger.comment("前置数据准备");
            int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            String BuyUserName = EnvSetup.getData("BuyUserName");
            String BuyUserPassWord = EnvSetup.getData("BuyUserPassword");
            // 买手信息
            int SellerId = Integer.valueOf(EnvSetup.getData("BuyerId"));
            String SellerName = EnvSetup.getData("BuyerName");
            // 买手登录
            Map m = AuthCallService.authLogin(SellerName, "123456");
            String sellerAccessToken = (String) m.get("accessToken");
            // 买家登录
            Map m2 = AuthCallService.authLogin(BuyUserName, "123456");
            String buyerAccessToken = (String) m.get("accessToken");

            YmtCallService ymt = new YmtCallService();
            /** 添加收货地址 ***/
            int AddressId;
            List AddressList = ymt.getAddressId(buyerAccessToken, UserId);
            if (AddressList.size() > 0) {
                AddressId = (Integer) AddressList.get(0);
            }
            else {
                ymt.addAddress(buyerAccessToken, UserId);
                AddressId = (Integer) ymt.getAddressId(buyerAccessToken, UserId).get(0);
            }
            Map map2 = AuthCallService.authLogin(BuyUserName, BuyUserPassWord);
            String AccessToken = (String) map2.get("AccessToken");
            // 创建产品
            /*** 创建直播商品 ***/
            // Map productinfomap = ProductCallService.createC2CProduct(sellerAccessToken, 1);
            Logger.comment("前置数据准备结束");
            /*** 获取产品信息 ***/
            /*
             * String productId = (String) (productinfomap.get("productId")); String catalogId =
             * (String) productinfomap.get("catalogId"); int activityId = (Integer)
             * productinfomap.get("activityId"); String sPicUrl =
             * String.valueOf(productinfomap.get("pic")); String ProductName =
             * String.valueOf(productinfomap.get("productDesc")); BigDecimal fQuotePrice =
             * (BigDecimal) productinfomap.get("price");
             */

            /*** 获取产品信息 ***/
            String productId = TestDataManager.getRandomUUID().toString();
            String catalogId = TestDataManager.getRandomUUID().toString();
            int activityId = 123456789;
            String sPicUrl = "http://p243.img.ymatou.com:10243/upload/productdes/3afa103d4ec4466f824b9f0458d6f4e2.jpg";
            String ProductName = "接口自动化测试Test";
            BigDecimal fQuotePrice = new BigDecimal(160);
            appsaveorderbean.setActivityId(activityId);
            // 模板ID
            int ActivityTemplateId = 0;
            appsaveorderbean.setActivityTemplateId(ActivityTemplateId);
            appsaveorderbean.setAddressId(AddressId);
            // 应用终端来源
            appsaveorderbean.setAppTerminalSource(AppTerminalSourceEnum.PC);
            // 自动取消时间
            BigDecimal time = new BigDecimal("10");
            appsaveorderbean.setAutoCancelOrderMinutes(time);
            // 买家Id
            appsaveorderbean.setBuyerId(UserId);
            // 商品规格Id
            appsaveorderbean.setCatalogId(catalogId);
            // 佣金比率
            BigDecimal Rate = new BigDecimal("0.00");
            appsaveorderbean.setCommissionRate(Rate);
            // 是否使用优惠券
            appsaveorderbean.setCouponAvail4OrderDeduct(true);
            // 领用商家优惠券
            BigDecimal couponValue = new BigDecimal(10);
            String couponCode = new CouponCallService().receiveCouponInfo(SellerId, UserId, couponValue);
            // 优惠券code
            appsaveorderbean.setCouponCode(couponCode);
            // 商家优惠券
            appsaveorderbean.setCouponChannel(2);
            appsaveorderbean.setCouponUseType(1);
            appsaveorderbean.setCouponValue(couponValue);
            // 商品定金价格
            appsaveorderbean.setEarnest(new BigDecimal(100));
            // 是否免佣金
            appsaveorderbean.setFeeFree(true);
            // 是否使用红包
            appsaveorderbean.setGiftAvail4OrderDeduct(false);
            // 是否返红包
            appsaveorderbean.setGiftAvail4Reward(false);
            // 订单留言
            appsaveorderbean.setLeaveWord("自动化测试订单");
            // 物流类型
            appsaveorderbean.setLogisticsType(LogisticsTypeEnum.XloboBondedDelivery);
            // 是否为V账户
            appsaveorderbean.setOnly4Vip(false);
            // 订单来源
            appsaveorderbean.setOrderSource(OrderSourceEnum.PC);
            // 订单类型
            appsaveorderbean.setOrderType(OrderTypeEnum.Shangou);
            // 商品规格
            String ProductCatalog = "户外装备";
            appsaveorderbean.setProductCatalog(ProductCatalog);
            // 商品Id
            appsaveorderbean.setProductId(productId);
            // 商品名称
            appsaveorderbean.setProductName(ProductName);
            // 购买的商品数
            int ProductNum = 1;
            appsaveorderbean.setProductNum(ProductNum);
            // 商品的图片地址
            appsaveorderbean.setProductPicUrl(sPicUrl);
            // 订单商品价格
            appsaveorderbean.setProductPrice(fQuotePrice);
            // 促销金额
            BigDecimal Promotion = new BigDecimal("0");
            appsaveorderbean.setPromotion(Promotion);
            // 促销类型
            int PromotionType = 0;
            appsaveorderbean.setPromotionType(PromotionType);
            // 卖家用户Id
            appsaveorderbean.setSellerId(SellerId);
            // 订单来源IP
            String ip = "192.168.0.1";
            appsaveorderbean.setSourceIP(ip);
            // 终端来源
            appsaveorderbean.setTerminalSource(TerminalSourceEnum.IOS);
            // 使用红包的数量
            int UseGiftAmount = 0;
            appsaveorderbean.setUseGiftAmount(UseGiftAmount);
            // 发送请求
            appsaveordercall.setData(appsaveorderbean);
            appsaveordercall.callService();
            Logger.verifyEquals(ECode.SUCCESS, appsaveordercall.getCode(), "验证Code");
            Logger.verifyNotNull(appsaveordercall.getOrderId(), "验证订单号不为空");
            // 数据库验证
            verifyDb();
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_AppSaveOrder_004() {
        Logger.start(true, "App保存闪购订单,使用平台优惠券");
        try {
            /*** 读取测试数据 ***/
            Logger.comment("前置数据准备");
            int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            String BuyUserName = EnvSetup.getData("BuyUserName");
            String BuyUserPassWord = EnvSetup.getData("BuyUserPassword");
            // 买手信息
            int SellerId = Integer.valueOf(EnvSetup.getData("BuyerId"));
            String SellerName = EnvSetup.getData("BuyerName");
            // 买手登录
            Map m = AuthCallService.authLogin(SellerName, "123456");
            String sellerAccessToken = (String) m.get("accessToken");
            // 买家登录
            Map m2 = AuthCallService.authLogin(BuyUserName, "123456");
            String buyerAccessToken = (String) m.get("accessToken");

            YmtCallService ymt = new YmtCallService();
            /** 添加收货地址 ***/
            int AddressId;
            List AddressList = ymt.getAddressId(buyerAccessToken, UserId);
            if (AddressList.size() > 0) {
                AddressId = (Integer) AddressList.get(0);
            }
            else {
                ymt.addAddress(buyerAccessToken, UserId);
                AddressId = (Integer) ymt.getAddressId(buyerAccessToken, UserId).get(0);
            }
            Map map2 = AuthCallService.authLogin(BuyUserName, BuyUserPassWord);
            String AccessToken = (String) map2.get("AccessToken");
            // 创建产品
            /*** 创建直播商品 ***/
            // Map productinfomap = ProductCallService.createC2CProduct(sellerAccessToken, 1);
            Logger.comment("前置数据准备结束");
            /*** 获取产品信息 ***/
            /*
             * String productId = (String) (productinfomap.get("productId")); String catalogId =
             * (String) productinfomap.get("catalogId"); int activityId = (Integer)
             * productinfomap.get("activityId"); String sPicUrl =
             * String.valueOf(productinfomap.get("pic")); String ProductName =
             * String.valueOf(productinfomap.get("productDesc")); BigDecimal fQuotePrice =
             * (BigDecimal) productinfomap.get("price");
             */

            /*** 获取产品信息 ***/
            String productId = TestDataManager.getRandomUUID().toString();
            String catalogId = TestDataManager.getRandomUUID().toString();
            int activityId = 123456789;
            String sPicUrl = "http://p243.img.ymatou.com:10243/upload/productdes/3afa103d4ec4466f824b9f0458d6f4e2.jpg";
            String ProductName = "接口自动化测试Test";
            BigDecimal fQuotePrice = new BigDecimal(160);

            appsaveorderbean.setActivityId(activityId);
            // 模板ID
            int ActivityTemplateId = 0;
            appsaveorderbean.setActivityTemplateId(ActivityTemplateId);
            appsaveorderbean.setAddressId(AddressId);
            // 应用终端来源
            appsaveorderbean.setAppTerminalSource(AppTerminalSourceEnum.PC);
            // 自动取消时间
            BigDecimal time = new BigDecimal("10");
            appsaveorderbean.setAutoCancelOrderMinutes(time);
            // 买家Id
            appsaveorderbean.setBuyerId(UserId);
            // 商品规格Id
            appsaveorderbean.setCatalogId(catalogId);
            // 佣金比率
            BigDecimal Rate = new BigDecimal("0.00");
            appsaveorderbean.setCommissionRate(Rate);
            // 是否使用优惠券
            appsaveorderbean.setCouponAvail4OrderDeduct(true);
            // 领用商家优惠券
            BigDecimal couponValue = new BigDecimal(10);
            String couponCode = new OrderCallService().receivePlatformCoupon(UserId, couponValue);
            // 优惠券code
            appsaveorderbean.setCouponCode(couponCode);
            // 平台优惠券
            appsaveorderbean.setCouponChannel(1);
            appsaveorderbean.setCouponUseType(1);
            appsaveorderbean.setCouponValue(couponValue);
            // 商品定金价格
            appsaveorderbean.setEarnest(new BigDecimal(100));
            // 是否免佣金
            appsaveorderbean.setFeeFree(true);
            // 是否使用红包
            appsaveorderbean.setGiftAvail4OrderDeduct(false);
            // 是否返红包
            appsaveorderbean.setGiftAvail4Reward(false);
            // 订单留言
            appsaveorderbean.setLeaveWord("自动化测试订单");
            // 物流类型
            appsaveorderbean.setLogisticsType(LogisticsTypeEnum.XloboBondedDelivery);
            // 是否为V账户
            appsaveorderbean.setOnly4Vip(false);
            // 订单来源
            appsaveorderbean.setOrderSource(OrderSourceEnum.PC);
            // 订单类型
            appsaveorderbean.setOrderType(OrderTypeEnum.Shangou);
            // 商品规格
            String ProductCatalog = "户外装备";
            appsaveorderbean.setProductCatalog(ProductCatalog);
            // 商品Id
            appsaveorderbean.setProductId(productId);
            // 商品名称
            appsaveorderbean.setProductName(ProductName);
            // 购买的商品数
            int ProductNum = 1;
            appsaveorderbean.setProductNum(ProductNum);
            // 商品的图片地址
            appsaveorderbean.setProductPicUrl(sPicUrl);
            // 订单商品价格
            appsaveorderbean.setProductPrice(fQuotePrice);
            // 促销金额
            BigDecimal Promotion = new BigDecimal("0");
            appsaveorderbean.setPromotion(Promotion);
            // 促销类型
            int PromotionType = 0;
            appsaveorderbean.setPromotionType(PromotionType);
            // 卖家用户Id
            appsaveorderbean.setSellerId(SellerId);
            // 订单来源IP
            String ip = "192.168.0.1";
            appsaveorderbean.setSourceIP(ip);
            // 终端来源
            appsaveorderbean.setTerminalSource(TerminalSourceEnum.IOS);
            // 使用红包的数量
            int UseGiftAmount = 0;
            appsaveorderbean.setUseGiftAmount(UseGiftAmount);
            // 发送请求
            appsaveordercall.setData(appsaveorderbean);
            appsaveordercall.callService();
            Logger.verifyEquals(ECode.SUCCESS, appsaveordercall.getCode(), "验证Code");
            Logger.verifyNotNull(appsaveordercall.getOrderId(), "验证订单号不为空");
            // 数据库验证
            verifyDb();
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_AppSaveOrder_005() {
        Logger.start(true, "优惠券不存在");
        try {
            /*** 读取测试数据 ***/
            Logger.comment("前置数据准备");
            int UserId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
            String BuyUserName = EnvSetup.getData("BuyUserName");
            String BuyUserPassWord = EnvSetup.getData("BuyUserPassword");
            // 买手信息
            int SellerId = Integer.valueOf(EnvSetup.getData("BuyerId"));
            String SellerName = EnvSetup.getData("BuyerName");
            // 买手登录
            Map m = AuthCallService.authLogin(SellerName, "123456");
            String sellerAccessToken = (String) m.get("accessToken");
            // 买家登录
            Map m2 = AuthCallService.authLogin(BuyUserName, "123456");
            String buyerAccessToken = (String) m.get("accessToken");

            YmtCallService ymt = new YmtCallService();
            /** 添加收货地址 ***/
            int AddressId;
            List AddressList = ymt.getAddressId(buyerAccessToken, UserId);
            if (AddressList.size() > 0) {
                AddressId = (Integer) AddressList.get(0);
            }
            else {
                ymt.addAddress(buyerAccessToken, UserId);
                AddressId = (Integer) ymt.getAddressId(buyerAccessToken, UserId).get(0);
            }
            Map map2 = AuthCallService.authLogin(BuyUserName, BuyUserPassWord);
            String AccessToken = (String) map2.get("AccessToken");
            // 创建产品
            /*** 创建直播商品 ***/
            // Map productinfomap = ProductCallService.createC2CProduct(sellerAccessToken, 1);
            Logger.comment("前置数据准备结束");
            /*** 获取产品信息 ***/
            /*
             * String productId = (String) (productinfomap.get("productId")); String catalogId =
             * (String) productinfomap.get("catalogId"); int activityId = (Integer)
             * productinfomap.get("activityId"); String sPicUrl =
             * String.valueOf(productinfomap.get("pic")); String ProductName =
             * String.valueOf(productinfomap.get("productDesc")); BigDecimal fQuotePrice =
             * (BigDecimal) productinfomap.get("price");
             */

            /*** 获取产品信息 ***/
            String productId = TestDataManager.getRandomUUID().toString();
            String catalogId = TestDataManager.getRandomUUID().toString();
            int activityId = 123456789;
            String sPicUrl = "http://p243.img.ymatou.com:10243/upload/productdes/3afa103d4ec4466f824b9f0458d6f4e2.jpg";
            String ProductName = "接口自动化测试Test";
            BigDecimal fQuotePrice = new BigDecimal(160);

            appsaveorderbean.setActivityId(activityId);
            // 模板ID
            int ActivityTemplateId = 0;
            appsaveorderbean.setActivityTemplateId(ActivityTemplateId);
            appsaveorderbean.setAddressId(AddressId);
            // 应用终端来源
            appsaveorderbean.setAppTerminalSource(AppTerminalSourceEnum.PC);
            // 自动取消时间
            BigDecimal time = new BigDecimal("10");
            appsaveorderbean.setAutoCancelOrderMinutes(time);
            // 买家Id
            appsaveorderbean.setBuyerId(UserId);
            // 商品规格Id
            appsaveorderbean.setCatalogId(catalogId);
            // 佣金比率
            BigDecimal Rate = new BigDecimal("0.00");
            appsaveorderbean.setCommissionRate(Rate);
            // 是否使用优惠券
            appsaveorderbean.setCouponAvail4OrderDeduct(true);
            // 领用商家优惠券
            BigDecimal couponValue = new BigDecimal(10);
            String couponCode = "1111111";
            // 优惠券code
            appsaveorderbean.setCouponCode(couponCode);
            // 平台优惠券
            appsaveorderbean.setCouponChannel(1);
            appsaveorderbean.setCouponUseType(1);
            appsaveorderbean.setCouponValue(couponValue);
            // 商品定金价格
            appsaveorderbean.setEarnest(new BigDecimal(100));
            // 是否免佣金
            appsaveorderbean.setFeeFree(true);
            // 是否使用红包
            appsaveorderbean.setGiftAvail4OrderDeduct(false);
            // 是否返红包
            appsaveorderbean.setGiftAvail4Reward(false);
            // 订单留言
            appsaveorderbean.setLeaveWord("自动化测试订单");
            // 物流类型
            appsaveorderbean.setLogisticsType(LogisticsTypeEnum.XloboBondedDelivery);
            // 是否为V账户
            appsaveorderbean.setOnly4Vip(false);
            // 订单来源
            appsaveorderbean.setOrderSource(OrderSourceEnum.PC);
            // 订单类型
            appsaveorderbean.setOrderType(OrderTypeEnum.Shangou);
            // 商品规格
            String ProductCatalog = "户外装备";
            appsaveorderbean.setProductCatalog(ProductCatalog);
            // 商品Id
            appsaveorderbean.setProductId(productId);
            // 商品名称
            appsaveorderbean.setProductName(ProductName);
            // 购买的商品数
            int ProductNum = 1;
            appsaveorderbean.setProductNum(ProductNum);
            // 商品的图片地址
            appsaveorderbean.setProductPicUrl(sPicUrl);
            // 订单商品价格
            appsaveorderbean.setProductPrice(fQuotePrice);
            // 促销金额
            BigDecimal Promotion = new BigDecimal("0");
            appsaveorderbean.setPromotion(Promotion);
            // 促销类型
            int PromotionType = 0;
            appsaveorderbean.setPromotionType(PromotionType);
            // 卖家用户Id
            appsaveorderbean.setSellerId(SellerId);
            // 订单来源IP
            String ip = "192.168.0.1";
            appsaveorderbean.setSourceIP(ip);
            // 终端来源
            appsaveorderbean.setTerminalSource(TerminalSourceEnum.IOS);
            // 使用红包的数量
            int UseGiftAmount = 0;
            appsaveorderbean.setUseGiftAmount(UseGiftAmount);
            // 发送请求
            appsaveordercall.setData(appsaveorderbean);
            appsaveordercall.callService();
            Logger.verifyEquals(ECode.FAILED201, appsaveordercall.getCode(), "验证Code");
            Logger.verifyEquals("券码不存在", appsaveordercall.getMsg(), "验证Code");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
